package com.ruoyi.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.HashMap;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.customer.domain.bo.GradeConfigBo;
import com.ruoyi.customer.domain.vo.GradeConfigVo;
import com.ruoyi.customer.domain.GradeConfig;
import com.ruoyi.customer.mapper.GradeConfigMapper;
import com.ruoyi.customer.service.IGradeConfigService;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 业绩星级Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-20
 */
@RequiredArgsConstructor
@Service
public class GradeConfigServiceImpl implements IGradeConfigService {

    private final GradeConfigMapper baseMapper;

    /**
     * 查询业绩星级
     */
    @Override
    public GradeConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询业绩星级列表
     */
    @Override
    public TableDataInfo<GradeConfigVo> queryPageList(GradeConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GradeConfig> lqw = buildQueryWrapper(bo);
        Page<GradeConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询业绩星级列表
     */
    @Override
    public List<GradeConfigVo> queryList(GradeConfigBo bo) {
        LambdaQueryWrapper<GradeConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GradeConfig> buildQueryWrapper(GradeConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GradeConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStartPrice()), GradeConfig::getStartPrice, bo.getStartPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getEndPrice()), GradeConfig::getEndPrice, bo.getEndPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getGrade()), GradeConfig::getGrade, bo.getGrade());
        lqw.eq(bo.getSort() != null, GradeConfig::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getRatio()), GradeConfig::getRatio, bo.getRatio());
        lqw.eq(StringUtils.isNotBlank(bo.getUserType()), GradeConfig::getUserType, bo.getUserType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), GradeConfig::getStatus, bo.getStatus());
        lqw.eq(bo.getCreateId() != null, GradeConfig::getCreateId, bo.getCreateId());
        lqw.orderByAsc(GradeConfig::getGrade);
        return lqw;
    }

    /**
     * 新增业绩星级
     */
    @Override
    public Boolean insertByBo(GradeConfigBo bo) {
        GradeConfig add = BeanUtil.toBean(bo, GradeConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改业绩星级
     */
    @Override
    public Boolean updateByBo(GradeConfigBo bo) {
        GradeConfig update = BeanUtil.toBean(bo, GradeConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GradeConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除业绩星级
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Map<String, String> gradeInfo(String revenue) {
        Map<String, String> map = new HashMap<>();
        map.put("grade", "1");
        map.put("gradeName", "");
        if (!StringUtils.isNotBlank(revenue)) {
            return map;
        }
        List<GradeConfig> gradeConfigs = baseMapper.gradeInfo(revenue);
        if (!CollectionUtils.isEmpty(gradeConfigs)) {
            map.put("grade", gradeConfigs.get(0).getGrade());
            map.put("gradeName", gradeConfigs.get(0).getGradeName());
            return map;
        }
        return map;
    }
}
